<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\userrecharge\admin\controller;

use think\facade\View;
use app\admin\BaseController;
use app\admin\model\User;
use plugins\userrecharge\admin\model\UserRecharge;
/**
 * 充值记录
 */
class Index extends BaseController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $input = input();
            $search = ['date', 'status', 'keyword'];
            $append = ['method','c_status'];
            $order = ! empty($input['prop']) && ! empty($input['order']) ? [$input['prop'] => $input['order']] : ['create_time' => 'desc'];
            $count = UserRecharge::withSearch($search, $input)->count();
            $data  = UserRecharge::withSearch($search, $input)->with(['user'])->order($order)->page($input['page'], $input['pageSize'])->append($append)->select();
            return json(['status' => 'success','message' => '获取成功', 'data' => $data, 'count' => $count]);
        } else {
            return View::fetch();
        }
    }
    
    /**
     * 修改订单为已付款
     */
    public function confirmPayment()
    {
        if ($this->request->isPost()) {
            $input = input("post.");
            $recharge = UserRecharge::where('id', $input['id'])->find();
            $recharge->status = 3;
            $recharge->save();
            $user = User::where('id', $recharge->user_id)->find();
            $user->balance = $user->balance + $recharge->pay_rmb;
            $user->save();
            return json(["status" => "success", "message" => "付款成功"]);
        }
    }
}